//
//  ViewController.m
//  UI-10-作业
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) NSDictionary *dictionary;
// 排序后的keys数组
@property (nonatomic, retain) NSArray *keysArray;

@end

@implementation ViewController
- (void)dealloc{
    [_dictionary release];
    [_keysArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dictionary = [[NSDictionary alloc]init];
    self.keysArray = [[NSArray alloc]init];
    
    [self cinfig];
    [self handleDate];
    
    [self creatTableView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)cinfig{
    self.navigationItem.title = @"联系人";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor blackColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark - 数据处理
-(void)handleDate{
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Cuntact" ofType:@"plist"];
    //从文件中获取数据
    self.dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    self.keysArray = [self.dictionary.allKeys sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", _keysArray);
}

#pragma mark - UITableView
-(void)creatTableView{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 ,CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64) style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:tableView];
    /**核心API*/
    tableView.delegate = self;
    tableView.dataSource = self;
    //tableView 常用API
    //行高
    tableView.rowHeight = 100;
    
    //设置分割线
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    tableView.separatorColor =[UIColor redColor];
    
    //建设重用池
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}




#pragma mark - UITableViewDataSource 协议
/**和数据相关的*/
//返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dictionary.count;
}


//返回分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    //根据section取出分区对应的联系人分组key
    NSString *key = [self.keysArray objectAtIndex:section];
    //再根据key 取出对应联系人分组
    NSArray *arr = [self.dictionary objectForKey:key];
    return arr.count;
}
//返回每一行上cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //从重用池中得到cell, 如果重用池中没有cell, 系统就会自动创就按cell并返回
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //根据section取出联系人分组
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
    //根据 row取出联系人分组
    NSDictionary *dic = [array objectAtIndex:indexPath.row];
    
    
    cell.textLabel.text = [dic objectForKey:@"name"];
    NSString *nss = [dic objectForKey:@"tupian"];
    cell.imageView.image = [UIImage imageNamed:nss];
    cell.detailTextLabel.text = @"lanou";
    //    cell.accessoryType = UITableViewCellAccessoryDetailButton;
    //    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    
    return cell ;
}
//返回分区头的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *headerString = [self.keysArray objectAtIndex:section];
    return headerString;
}
//返回分区尾标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"footer";
}



#pragma mark - UITableViewDelegate 协议
/*控制视图行为的*/

//返回行高方法
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    switch (indexPath.row) {
//        case 0:
//            return 100;
//            break;
//
//        default:
//            return 50;
//            break;
//    }
//
//}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        SecondViewController * secondVC = [[SecondViewController alloc]init];
//    [self.dictionary allKeys];
    NSArray *array = [self.dictionary objectForKey:[self.keysArray objectAtIndex:indexPath.section]];
   secondVC.diction = [array objectAtIndex:indexPath.row];
    
        [self.navigationController pushViewController:secondVC animated:YES];
 
}



//返回分区尾的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 50.0;
//}
//侧边栏
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return self.keysArray;
}


@end
